<?php
namespace app\common\dao\users;

use app\common\dao\BaseDao;
use app\common\model\users\UsersModel;

/**
 * Users数据访问层
 */
class UsersDao extends BaseDao
{
    /**
     * 获取模型类名
     * @return string
     */
    protected function getModel(): string 
    {
        return UsersModel::class;
    }

    /**
     * 搜索构造器
     * @param array $where 查询条件
     * @return \think\db\Query
     */
    public function search(array $where = [])
    {
        $query = UsersModel::getDB()
            ->when(isset($where['id']) && !empty($where['id']), function ($query) use($where) {
                if (is_array($where['id'])) {
                    $query->whereIn('id', $where['id']);
                } else {
                    $query->where('id', $where['id']);
                }
            })->when(isset($where['password']) && $where['password'] !== '', function ($query) use ($where) {
                $query->whereLike('password', '%' . trim($where['password']) . '%');
            })->when(isset($where['password_key']) && $where['password_key'] !== '', function ($query) use ($where) {
                $query->where('password', $where['password_key']);
            })->when(isset($where['email']) && $where['email'] !== '', function ($query) use ($where) {
                $query->whereLike('email', '%' . trim($where['email']) . '%');
            })->when(isset($where['email_key']) && $where['email_key'] !== '', function ($query) use ($where) {
                $query->where('email', $where['email_key']);
            })->when(isset($where['token']) && $where['token'] !== '', function ($query) use ($where) {
                $query->whereLike('token', '%' . trim($where['token']) . '%');
            })->when(isset($where['token_key']) && $where['token_key'] !== '', function ($query) use ($where) {
                $query->where('token', $where['token_key']);
            })->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use($where) {
                $query->where('is_del', $where['is_del']);
            })->when(isset($where['status']) && $where['status'] !== '', function ($query) use($where) {
                $query->where('status', $where['status']);
            });

        return $query;
    }
}
